package com.lgmshare.k3.map;

/**
 * 地图坐标转换工具类
 * <p/>
 * Created by Administrator on 2016/2/16.
 */
public class MapCoordinateConverterUtil {

    private static double x_pi = Math.PI * 3000 / 180;

    /**
     * 将 GCJ-02 坐标转换成 BD-09 坐标
     * GoogleMap和高德map用的是同一个坐标系GCJ-02
     */
    public static double[] gcj02_bd09(double gg_lat, double gg_lon) {
        double bd_lat = 0.0;
        double bd_lon = 0.0;
        double[] location = new double[2];
        double x = gg_lon, y = gg_lat;
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
        location[0] = z * Math.sin(theta) + 0.006;
        location[1] = z * Math.cos(theta) + 0.0065;
        return location;
    }

    /**
     * 将 BD-09 坐标转换成 GCJ-02 坐标
     * GoogleMap和高德map用的是同一个坐标系GCJ-02
     */
    public static double[] bd09_gcj02(double bd_lat, double bd_lon) {
        double[] location = new double[2];
        double x = bd_lon - 0.0065, y = bd_lat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
        location[0] = z * Math.sin(theta);
        location[1] = z * Math.cos(theta);
        return location;
    }

    /**
     * 经纬度格式化
     *
     * @param lat
     * @param lng
     * @return
     */
    public static double[] parserLatLng(String lat, String lng) {
        double[] location = new double[2];
        try {
            location[0] = Double.valueOf(lat);
            location[1] = Double.valueOf(lng);
            return location;
        } catch (NumberFormatException ne) {
            ne.printStackTrace();
        }
        return location;
    }
}
